package com.fh.shop.api.cart.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.cart.biz.ICartService;

import com.fh.shop.api.cart.vo.CartVo;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConst;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/carts")
@Api(tags = "购物车接口")
public class CartController {

    @Resource
    private HttpServletRequest request;

    @Resource(name = "cartService")
    private ICartService cartService;


    //增加购物车商品
    @PostMapping
    @NeedLogin
    @ApiOperation("添加商品到购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId",value = "商品id",dataType = "java.lang.Long",example = "0",required = true),
            @ApiImplicitParam(name = "count",value = "商品数量",dataType = "java.lang.Long",example = "0",required = true),
            @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    })
    public ServerResponse addItem(Long productId,Long count){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.addItem(productId,count,memberId);
    }

    //查找购物车
    @GetMapping
    @NeedLogin
    @ApiOperation("显示购物车信息")
    @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    public ServerResponse findCart(){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        //String cartJson = RedisUtil.get(KeyUtil.buildCartKey(memberId));
        String cartJson = RedisUtil.hget(KeyUtil.buildCartKey(memberId),SystemConst.CART_MCONENT);
        CartVo cartVo = JSONObject.parseObject(cartJson, CartVo.class);
        return ServerResponse.success(cartVo);
    }

    @DeleteMapping("{id}")
    @NeedLogin
    @ApiOperation("删除购物车中的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "删除商品id",dataType = "java.lang.Long",example = "0",required = true),
            @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    })
    public ServerResponse delete(@PathVariable Long id){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.deleteById(id,memberId);
    }

    @DeleteMapping
    @NeedLogin
    @ApiOperation("批量删除购物车中的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "批量删除商品id",dataType = "java.lang.String",required = true),
            @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    })
    public ServerResponse deleteBatch(String ids){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.deleteBatch(ids,memberId);
    }

    @GetMapping("/findCartItemCount")
    @NeedLogin
    @ApiOperation("获取购物车中商品的数量")
    @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    public ServerResponse findCartItemCount(){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.findCartItemCount(memberId);
    }


}
